import * as signalR from "@aspnet/signalr";

export default {
    namespaced: true, //命名空间
    state: {
        token: "",
        systemMessageList: [],
        userMessageList: [],
        Connection: "",
    },
    getters: {},
    mutations: {
        StartConnection(state) {
            this.token = window.sessionStorage.getItem("accessToken").replace(/"/g, "");
            state.Connection = new signalR.HubConnectionBuilder()
                .withUrl("http://localhost:21021/signalr", {
                    accessTokenFactory: () => this.token,
                })
                .configureLogging(signalR.LogLevel.Information)
                .build();
            state.Connection.start();
        },
        //获取系统消息
        GetSystemMessage(state, name) {
            if (state.Connection.connectionState == 1) {
                state.Connection.on(name, (message) => {
                    state.systemMessageList.push(message);
                })
            }
        },
        //获取用户消息
        GetUserMessage(state, name) {
            if (state.Connection.connectionState == 1) {
                state.Connection.on(name, (message) => {
                    state.userMessageList.push(message);
                })
            }
        },
        //发送消息
        SendMessage(state, name, message) {
            if (state.Connection.connectionState == 1) {
                state.Connection.on(name, message)
            }
        },
        //通知
        invokeNotice(state, name) {
            if (state.Connection.connectionState == 1) {
                state.Connection.invoke(name)
            }
        },
        //通知并发送消息
        invokeNoticeAndSend(state, name, message) {
            if (state.Connection.connectionState == 1) {
                state.Connection.invoke(name, message)
            }
        }
    },
    actions: {},
    modules: {},
};